$(function () {

    //for selection encode
    chrome.contextMenus.create({
        "title": "Convert TCAG to HEX",
        "type": "normal",
        "contexts": ["selection"],
        //"contexts": ["page", "frame", "editable", "image", "video", "audio", "link", "selection"],
        "onclick": function (info, tab) {
            //var txt = info.selectionText.replace(/ /g, "");
            if (info.selectionText.match(/[^TCAG ]/)) {
                //alert("Please, select sequence only");
                alert("Sequence only will be processed");
            }
            var txt = info.selectionText.replace(/[^TCAG]/gi, "").toUpperCase(); // remove all but TCAG characters
            var outstr = "";
            for (var i = 0; i <= txt.length - 4; i = i + 4) {
                //var inchar = txt.substr(i, 1);
                //var out = "TCAG".indexOf(inchar);
                var out = Nuc2Quat(txt.substr(i, 1)) * 4 * 4 * 4
                            + Nuc2Quat(txt.substr(i + 1, 1)) * 4 * 4
                            + Nuc2Quat(txt.substr(i + 2, 1)) * 4
                            + Nuc2Quat(txt.substr(i + 3, 1));
                outstr = outstr + ("00" + out.toString(16).toUpperCase()).substr(-2) + " ";
                if (i % 64 == 60) {
                    //outstr = outstr + "<br/>"; // "\n";
                    outstr = outstr + "\n";
                }
            }
            alert(outstr);

//            var myWindow = window.open(chrome.extension.getURL('../html/popup.html'), "_blank",
//            "toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, copyhistory=yes, width=600, height=400, left=200, top=200");
//            //var elem = myWindow.document.getElementById("hexData");
//            var elem = myWindow.opener.$("#hexData");
//            alert(elem.text());
//            //elem.style.color = "red";
//            elem.append(outstr);
//            elem.html(outstr.replace("\n", "<br/>"));
//            myWindow.focus();



            //            $("#dialog").dialog({ autoOpen: false, modal: false,  title: "Dialog Title" });
            //            $("#dialog").dialog("open");

            //            var myWindow = window.open('', "_blank",
            //            "toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, copyhistory=yes, width=600, height=400, left=200, top=200");
            //            myWindow.document.body.style.fontFamily = "'Courier New',Courier,monospace";
            //            //alert(myWindow.document.body.style.cssText);
            //            myWindow.document.write("<p>" + outstr.replace("\n", "<br/>") + "</p>");
            //            myWindow.focus();

        }
    });


});

function Nuc2Quat(a) {
    return "TCAG".indexOf(a) + "".indexOf(a);
}